limsSampleHandle.editPage = function (pageType) {

    return {
        getGridParam: function () {
            let _this = this;
            let columns = limsSampleHandle.getEditListColumns(pageType); // 出库、销毁
            if (pageType === 'back') {
                columns = limsSampleHandle.getBackEditListColumns();  // 返样
            }
            let gridStruct = {
                type: 'grid',
                id: _this.idManage.handleGridId,
                url: limsSampleHandle.baseUrl + '/queries/raw',
                requestData: {
                    handleType_EQ: _this.param.handleType,
                    createdById_EQ: workspace.user.id,
                    createdByOrgId_EQ: workspace.user.ext$.loginorgid
                },
                service: 'limsSampleHandleServiceImpl',
                dbTable: 'T_LIMS_SAMPLE_HANDLE',
                columns: columns,
                onLoadSuccess: function (data) {
                    if (!Gikam.isEmpty(data)) {
                        this.activeRowByIndex(0);
                    } else {
/*                        Gikam.cleanCompData([_this.idManage.sampleGridId, _this.idManage.flowGridId]);
                        Gikam.getAsyncComp(_this.idManage.uploaderId).done(function (comp) {
                            comp.setOptions({
                                bizId: -1,
                            });
                        });*/
                    }
                },
                page: true,
                deleteFormatter: function (row) {
                    return row.handleCode
                },
                onRowActive: function (index, row) {
/*                    Gikam.getAsyncComp(_this.idManage.sampleGridId).done(function (grid) {
                        grid.refresh({
                            url: limsSampleHandle.sampleUrl + '/queries/sampleHandles',
                            requestData: {
                                sampleHandleId_EQ: row.id
                            }
                        });
                    });
                    Gikam.getAsyncComp(_this.idManage.uploaderId).done(function (comp) {
                        comp.setOptions({
                            bizId: row.id,
                        });
                    });
                    Gikam.getAsyncComp(_this.idManage.flowGridId).done(function (comp) {
                        comp.refresh({
                            url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_SAMPLE_HANDLE&id=' + row.id
                        });
                    });*/
                },
                toolbar: [{
                    type: 'button',
                    text: 'GIKAM.BUTTON.INSERT',
                    icon: 'add',
                    onClick: function () {
                        Gikam.preInsert({
                            modalTitle: 'GIKAM.AT.INSERT',
                            titleWidth: 80,
                            fields: [{
                                field: 'remark',
                                title: 'T_LIMS_SAMPLE_HANDLE.REMARK',
                                type: 'textarea',
                                height: 100,
                                preInsert: true
                            }],
                            height: 250,
                            url: limsSampleHandle.baseUrl,
                            param: {
                                handleType: _this.param.handleType
                            }
                        }).done(function (id) {
                            Gikam.getComp(_this.idManage.handleGridId).refresh();
                            limsSampleHandle.loadSampleHandleDetailPage({id:id}, pageType, 'edit')
                        });
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.BUTTON.DELETE',
                    icon: 'remove-row',
                    onClick: function () {
                        Gikam.getComp(_this.idManage.handleGridId).deleteRows(limsSampleHandle.baseUrl);
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                    icon: 'submit', color: 'success',
                    onClick: function () {
                        _this.submit();
                    }
                }],
                onCellClick: function (field, row) {
                    field === 'handleCode' && limsSampleHandle.loadSampleHandleDetailPage(row, pageType, 'edit')
                },
            };

            if (pageType === 'back') {
                gridStruct.toolbar.push({
                    type: 'button',
                    text: 'GIKAM.BUTTON.COURIER.EDIT',
                    icon: 'edit',
                    onClick: function () {
                        _this.editCourierInfo();
                    }
                });
            } else if (pageType === 'out') {
            } else if (pageType === 'enter') {
            } else if (pageType === 'destroy') {
            }
            if (pageType === 'back') {
                // 样本返还客户申请
                gridStruct.toolbar.push({
                    type: 'button',
                    text: 'GIKAM.BUTTON.SAMPLE_BACK_ORDER',
                    icon: 'export',
                    onClick: function () {
                        var grid = Gikam.getComp(_this.idManage.handleGridId)
                        var selections = grid.getSelections()
                        if (Gikam.isEmpty(selections)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                            return
                        }
                        selections.forEach(r => {
                            var url = IFM_CONTEXT + '/core/module/item/files/template-urls' + Gikam.param({
                                templateCode: 'LimsSampleHandleExportDocxTemplate-' + pageType,
                                service: 'LimsSampleHandleExportDocxTemplate',
                                id: r.id
                            })
                            Gikam.getText(url).done(function (url) {
                                Gikam.download(IFM_CONTEXT + url, false)
                            }).always(function () {
                                workspace.window.closeMask()
                            })
                        })
                    }
                })
            } else if (pageType === 'out') {
                // 样本出库申请
                gridStruct.toolbar.push({
                    type: 'button',
                    text: 'GIKAM.BUTTON.SAMPLE_OUT_ORDER',
                    icon: 'export',
                    onClick: function () {
                        var grid = Gikam.getComp(_this.idManage.handleGridId)
                        var selections = grid.getSelections()
                        if (Gikam.isEmpty(selections)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                            return
                        }
                        selections.forEach(r => {
                            var url = IFM_CONTEXT + '/core/module/item/files/template-urls' + Gikam.param({
                                templateCode: 'LimsSampleHandleExportDocxTemplate-' + pageType,
                                service: 'LimsSampleHandleExportDocxTemplate',
                                id: r.id
                            })
                            Gikam.getText(url).done(function (url) {
                                Gikam.download(IFM_CONTEXT + url, false)
                            }).always(function () {
                                workspace.window.closeMask()
                            })
                        })
                    }
                })
            } else if (pageType === 'enter') {
                // 样本入库客户申请
                gridStruct.toolbar.push({
                    type: 'button',
                    text: 'GIKAM.BUTTON.SAMPLE_ENTER_ORDER',
                    icon: 'export',
                    onClick: function () {
                        var grid = Gikam.getComp(_this.idManage.handleGridId);
                        //入库单导出改为以选中行为准，因为系统导出貌似只能接收一个id，所以取选中的第1条
                        var selections = grid.getSelections()
                        if (Gikam.isEmpty(selections)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                            return
                        }
                        workspace.window.showMask()
                        selections.forEach(r => {
                            workspace.window.showMask()
                            var url = IFM_CONTEXT + '/core/module/item/files/template-urls' + Gikam.param({
                                templateCode: 'LimsSampleEnterExportTemplate',
                                service: 'LimsSampleEnterExportTemplate',
                                id: r.id
                            })
                            Gikam.getText(url).done(function (url) {
                                Gikam.download(IFM_CONTEXT + url, false)
                            }).always(function () {
                                workspace.window.closeMask()
                            })
                        })
                    }
                })
            } else if (pageType === 'destroy') {
                // 样本销毁申请
                gridStruct.toolbar.push({
                    type: 'button',
                    text: 'GIKAM.BUTTON.SAMPLE_DESTROY_ORDER',
                    icon: 'export',
                    onClick: function () {
                        var grid = Gikam.getComp(_this.idManage.handleGridId)
                        var selections = grid.getSelections()
                        if (Gikam.isEmpty(selections)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                            return
                        }
                        selections.forEach(r => {
                            var url = IFM_CONTEXT + '/core/module/item/files/template-urls' + Gikam.param({
                                templateCode: 'LimsSampleHandleExportDocxTemplate-' + pageType,
                                service: 'LimsSampleHandleExportDocxTemplate',
                                id: r.id
                            })
                            Gikam.getText(url).done(function (url) {
                                Gikam.download(IFM_CONTEXT + url, false)
                            }).always(function () {
                                workspace.window.closeMask()
                            })
                        })
                    }
                })
            }
            return gridStruct;
        },

        editCourierInfo : function () {
            var _this = this;
            let selections = Gikam.getComp(_this.idManage.handleGridId).getSelections();
            if (Gikam.isEmpty(selections)) {
                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                return;
            }
            Gikam.create('modal', {
                title: 'GIKAM.BUTTON.COURIER.EDIT',
                width: 400,
                height: 320,
                onAfterClose: function (data) {
                    if (Gikam.isNotEmpty(data)) {
                        workspace.window.showMask();
                        var chooseData = selections.map(function (item) {
                            return {
                                id: item.id,
                                courierNumber: data.courierNumber,
                                courierCompany: data.courierCompany,
                                recipient: data.recipient,
                                phone: data.phone,
                                address: data.address
                            }
                        })
                        Gikam.put(limsSampleHandle.baseUrl + '/instant', JSON.stringify({
                            b: ['limsSampleHandleServiceImpl', chooseData]
                        })).done(function (data) {
                            Gikam.getComp(_this.idManage.handleGridId).refresh();
                        }).always(function () {
                            workspace.window.closeMask();
                        });
                    }
                }
            });
            Gikam.create('layout', {
                renderTo: Gikam.getLastModal().window.$dom,
                center: {
                    items: [{
                        type: 'form',
                        id: 'pacakge-form',
                        columns: 1,
                        titleWidth: 80,
                        fields: [{
                            field: 'courierNumber',
                            title: 'T_LIMS_SAMPLE_HANDLE.COURIERNUMBER',
                            validators : ['notEmpty']
                        }, {
                            field: 'courierCompany',
                            title: 'T_LIMS_SAMPLE_HANDLE.COURIERCOMPANY',
                            firstBlank: false,
                            type: 'select',
                            category: 'sys',
                            param: {
                                codeCategoryId: 'courierCompany'
                            },
                            validators : ['notEmpty']
                        }, {
                            field: 'recipient',
                            title: 'T_LIMS_SAMPLE_HANDLE.RECIPIENT',
                            validators : ['notEmpty']
                        }, {
                            field: 'phone',
                            title: 'T_LIMS_SAMPLE_HANDLE.PHONE',
                            validators : ['notEmpty','phone']
                        }, {
                            field: 'address',
                            title: 'T_LIMS_SAMPLE_HANDLE.ADDRESS',
                            validators : ['notEmpty']
                        }],
                    }, {
                        type: 'btnToolbar',
                        items: [{
                            type: 'button',
                            text: 'GIKAM.BUTTON.CONFIRM',
                            class: 'blue',
                            icon: 'save',
                            color: 'white',
                            onClick: function () {
                                //校验判断
                                var form = Gikam.getComp('pacakge-form');
                                if (!form.validate()) {
                                    return;
                                }
                                Gikam.getLastModal().close(Gikam.getComp('pacakge-form').getData());
                            }
                        }, {
                            type: 'button',
                            text: 'GIKAM.BUTTON.CANCEL',
                            icon: 'back',
                            onClick: function () {
                                Gikam.getLastModal().close();
                            }
                        }]
                    }]
                }
            })
        },

        getSampleHandleInfo: function () {
            var _this = this;
            var toolBar = [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    let row = Gikam.getComp(_this.idManage.handleGridId).getActivedRow();
                    if (Gikam.isEmpty(row)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    // 出库、销毁、返样，必须是已入库的样本
                    var param = {
                        inHandle_EQ: '1',
                        handleType_EQ: 'enterDone'
                    };
                    if (_this.param.handleType === 'enter') {
                        // 未入库或出库的样本才能申请入库
                        param = {
                            inHandle_EQ: '0'
                        }
                    }
                    param.orgFilter = '1';
                    Gikam.create('modal', {
                        title: Gikam.choose.getConfig("sample-choose").title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig("sample-choose").url + Gikam.param(param),
                        onAfterClose: function (rows) {
                            if (Gikam.isEmpty(rows)) {
                                return;
                            }
                            var chooseData = rows.map(function (item) {
                                return {
                                    id: item.id,
                                    sampleHandleId: row.id,
                                    inHandle: '1',
                                    handleType: row.handleType
                                }
                            });
                            Gikam.getComp(_this.idManage.sampleGridId).insert(limsSampleHandle.baseUrl + '/sample/add', chooseData).done(function () {
                                Gikam.getComp(_this.idManage.sampleGridId).refresh();
                            });
                        }
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.REMOVE',
                icon: 'remove-row',
                onClick: function () {
                    let selections = Gikam.getComp(_this.idManage.sampleGridId).getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'GIKAM.CONFIRM.REMOVE', function () {
                        workspace.window.showMask();
                        Gikam.postText(limsSampleHandle.baseUrl + '/sample/delete',
                            Gikam.getJsonWrapper(null, [null, selections])).done(function () {
                            Gikam.getComp(_this.idManage.sampleGridId).refresh();
                        }).always(function () {
                            workspace.window.closeMask();
                        });
                    });
                }
            }]
            if (pageType === 'enter') {
            }
            return {
                type: 'tab',
                panels: [{
                    title: 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_INFO',
                    items: [{
                        type: 'grid',
                        id: _this.idManage.sampleGridId,
                        columns: limsSampleHandle.getSampleListColumns(false, pageType),
                        onCellClick: function (field, row) {
                            field === 'ext$.batchcode' && limsSampleHandle.loadBatchDetailPage(row);
                        },
                        onRendered: function () {
                            if (pageType !== 'enter') {
                                this.hideColumns(['ext$.sampleconcentration_temp', 'ext$.samplevolume_temp', 'ext$.restcount_temp'])
                            }
                        },
                        toolbar: toolBar
                    }]
                }, {
                    title: 'GIKAM.FILE.ATTACHMENT',
                    items: [{
                        type: 'uploader',
                        id: _this.idManage.uploaderId,
                        dbTable: 'T_LIMS_SAMPLE_HANDLE',
                        onBeforeUpload: function () {
                            var selection = Gikam.getComp(_this.idManage.handleGridId).getSelections();
                            if (!selection || Gikam.isEmpty(selection)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                                return false;
                            }
                        },
                        formatterGridColumns: function (columns) {
                            return Gikam.getFormatterGridColumns(columns);
                        }
                    }]
                }, {
                    title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                    items: [{
                        type: 'grid',
                        id: _this.idManage.flowGridId,
                        columns: limsSampleHandle.getFlowListColumns()
                    }]
                }]
            }
        },

        submit: function () {
            var _this = this;
            var grid = Gikam.getComp(_this.idManage.handleGridId);
            let selections = grid.getSelections();
            if (Gikam.isEmpty(selections)) {
                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                return
            }
            if (grid.validate(selections)) {
                Gikam.create('workflow').submit({
                    data: selections,
                    pageObject: limsSampleHandle,
                    formatterToolbar: (toolbar, type, formId) => {
                        toolbar.forEach(b => {
                            if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                                b.class = 'blue'
                                b.icon = 'save'
                                b.color = 'white'
                            } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                                b.icon = 'back'
                            }
                        })
                        return toolbar
                    }
                }).done(function () {
                    grid.refresh();
                });
            }
        },

        idManage: {
            handleGridId: pageType + 'lims-sample-handle-edit-list-grid',
            sampleGridId: pageType + 'lims-handle-sample-list-grid',
            uploaderId: pageType + 'sample-handle-uploader',
            flowGridId: pageType + 'handle-flow-edit-list-grid',
        },

        create: function () {
            var _this = this;
            Gikam.create('layout', {
                id: pageType + 'lims-sample-handle-edit-list-layout',
                renderTo: workspace.window.$dom,
                center: {
                    items: [this.getGridParam()]
                }
/*                north: {
                    height: '40%',
                    items: [this.getGridParam()]
                },
                center: {
                    items: [this.getSampleHandleInfo()]
                }*/
            });
        },

        init: function (param) {
            this.param = param;
            this.create();
        }
    }

};
